import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import common
import admin
import logging

template.register_template_library('templatefilters')

class HomeHandler(common.TemplateRequestHandler):
    def get(self):
        intro = common.columns.get_column_by_cid(common.COLUMN_TITLE_INTRO)
        membertitle = common.columns.get_title_by_cid(common.COLUMN_TITLE_MEMBER)
        members = common.members.get_members()
        self.generate('index.html', {'intro': intro, 'members': members,'membertitle': membertitle,})
        
class IntroHandler(common.TemplateRequestHandler):
    def get(self):
        column = common.columns.get_column_by_cid(common.COLUMN_TITLE_INTRO)
        if column is None:
            title = 'Introduction'
        else:
            title = column.title
        self.generate('intro.html', {'column_title': title, 'column':column,})
        
class SpecialistHandler(common.TemplateRequestHandler):
    def get(self):
        column = common.columns.get_column_by_cid(common.COLUMN_TITLE_SPECIALIST)
        items = common.columns.get_specialist_items()
        if column is None:
            title = 'Specialist'
        else:
            title = column.title
        self.generate('specialist.html', {'column_title': title,'column':column, 'items': items,})

class SpecialHandler(common.TemplateRequestHandler):
    def get(self, spekey):
        if not spekey:
            self.error(403)
            return
        key = None
        if spekey.find('?') > -1:
            items = spekey.split('?')
            spekey = items[0]
            if spekey.find('/') >-1:
                items = spekey.split('/')
                key = items[0]
            else:
                key = spekey
        else:
            key = spekey
        
        special = common.columns.get_by_id(long(key))
        if special is None:
            self.error(403)
            return
        column = common.columns.get_column_by_cid(common.COLUMN_TITLE_SPECIALIST)
        if column is None:
            title = 'Specialist'
        else:
            title = column.title
        
        self.generate('special.html', {'column_title': title, 'column':special,})
        
class ContactHandler(common.TemplateRequestHandler):
    def get(self):
        title = common.columns.get_title_by_cid(common.COLUMN_TITLE_CONTACT)
        if title is None:
            title = 'Contact US'
        hospital = common.setting.get(admin.SETTING_TITLE)
        address = common.setting.get(admin.SETTING_ADDRESS)
        telephone = common.setting.get(admin.SETTING_TELEPHONE)
        
        self.generate('contact.html', {'column_title': title,
                                       'hospital':hospital,
                                       'address': address,
                                       'telephone': telephone
                                       })

class GuideHandler(common.TemplateRequestHandler):
    def get(self):
        column = common.columns.get_column_by_cid(common.COLUMN_TITLE_GUIDE)
        self.generate('guide.html', {'column_title': column.title, 'column': column})

class KnowledgeBaseHandler(common.TemplateRequestHandler):
    def get(self):
        column = common.columns.get_column_by_cid(common.COLUMN_TITLE_KB)
        items = common.columns.get_kb_items(20)
        self.generate('kblist.html', {'column_title': column.title, 'column':column,'items': items,})
        
class KnowledgeBaseItemHandler(common.TemplateRequestHandler):
    def get(self, kbid):
        logging.info('kbid:%s'%kbid)
        if kbid.find('/') > -1:
            items = kbid.split('/')
            kbid = items[0]
        logging.info('new kbid:%s'%kbid)
        item = common.columns.get_by_id(long(kbid))
        logging.info('item:%s'%item)
        column = common.columns.get_column_by_cid(common.COLUMN_TITLE_KB)
        if column is None:
            title = 'Knowledge Base'
        else:
            title = column.title
        
        self.generate('kb.html', {'column_title': title, 'column':item,})
        
class MembersHandler(common.TemplateRequestHandler):
    def get(self):
        title = common.columns.get_title_by_cid(common.COLUMN_TITLE_MEMBER)
        members = common.members.get_members()
        if title is None:
            title = 'Members'
        self.generate('members.html', {'column_title': title,'items': members,})
        
class MemberHandler(common.TemplateRequestHandler):
    def get(self, spekey):
        if not spekey:
            self.error(403)
            return
        key = None
        if spekey.find('?') > -1:
            items = spekey.split('?')
            spekey = items[0]
            if spekey.find('/') >-1:
                items = spekey.split('/')
                key = items[0]
            else:
                key = spekey
        else:
            key = spekey
        
        member = common.members.get_by_id(long(key))
        if member is None:
            self.error(403)
            return
        column = common.columns.get_column_by_cid(common.COLUMN_TITLE_MEMBER)
        if column is None:
            title = 'Members'
        else:
            title = column.title
        
        self.generate('member.html', {'column_title': title, 'column':column, 'member': member,})
        
class PortraitHandler(common.TemplateRequestHandler):
    def get(self,key):
        if not key:
            self.redirect('/static/images/doctor_1.jpg')
        if key.find('.') >= 0:
            items = key.split('.')
            key = items[0]
        member = common.members.get(key)
        if member is None:
            self.error(403)
            return
        elif member.portrait:
            self.response.headers['Content-Type'] = 'image/jpeg'
            self.response.out.write(member.portrait)
        else:
            self.redirect('/static/images/doctor_1.jpg')
                       
def main():
    application = webapp.WSGIApplication([
            ('/', HomeHandler),
            ('/intro.*', IntroHandler),
            ('/specialist.*', SpecialistHandler),
            ('/contact.*', ContactHandler),
            ('/guide.*', GuideHandler),
            ('/kb.*', KnowledgeBaseHandler),
            ('/members.*', MembersHandler),
            (r'/member/(.*)', MemberHandler),
            (r'/special/(.*)', SpecialHandler),
            (r'/portrait/(.*)', PortraitHandler),
            (r'/kitem/(.*)', KnowledgeBaseItemHandler)
            ],debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()        